So im writing the C Programming Language, 2nd edition 1-24 exercise. I'm done with the states part of the programm.
The problem is, i dont know how to implement the function to check for the braces, parantheses and brackets errors.
Do i need a global array, or how can i get it done? I sure don't want to have the same code in 2 places in my code.
I asked on another forum, but they insist on making pointers, and push/pop functions. But i haven't worked with them yet, so i don't know how.
If anyone is willing to do it that way, just to fix it with pointers, it's fine with me aswell.
"Write a program to check a C program for rudimentary syntax errors
like unbalanced parentheses, brackets and braces. Don't forget about quotes,
both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.)"
Code:
#include <stdio.h>
#include <stdlib.h>
void checker(char c, int x);
int main(int argc, char *argv[])
{
enum states {normal, /* normal */
comment, comment_entry, comment_exit, /* comment stanja */
dquote, dquote_escape, /* " navodnici */
squote, squote2, squote_escape}; /* ' konstante */
int state = normal;
int c=0;
while ((c = getchar()) != EOF)
{
if (state == normal && c == '/') { state = comment_entry; }
else if(state==normal && c=='"') { state = dquote; }
else if(state==normal && c=='\'') { state = squote; }
else if (state == normal) { checker(c, x);}
else if (state == comment && c != '*') { continue; }
else if (state == comment) { state = comment_exit; continue; }
else if (state == comment_exit && c == '/') { state = normal; continue; }
else if (state == comment_exit && c == '*') { continue; }
else if (state == comment_exit) { state = comment; continue; }
else if (state == comment_entry && c == '*') { state = comment; continue; }
else if (state == comment_entry && c == '/') { }
else if (state == comment_entry) { state = normal; checker(c, x); }
else if(state==dquote && c=='"') { state = normal; }
else if(state==dquote && c=='\\') { state = dquote_escape; }
else if(state==dquote_escape) { state=dquote; continue; }
else if(state==squote && c=='\\') { state= squote_escape; }
else if(state==squote && c=='\'') { printf("Line %d: Constant error '' "); }
else if(state==squote && c!='\'' && c!='\\') { state=squote2; }
else if(state==squote_escape) { state= squote2; }
else if(state==squote2 && c=='\'') { state=normal; }
else if(state==squote2 && c!='\'') {printf("Line %d: Constant too long!"); }
}
getchar();
return 0;
}